﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using Object = UnityEngine.Object;

namespace SoarDEditor.Pandora
{
    public class PdrFindHelper : FindBase
    {
        #region Property Path
        public const string AssetsRootPath = "Assets";
        public static string UIPanelPath { get { return PdrConfig.Instance.UIPanelPath; } }
        public static string SourceAtlasPath { get { return ""; } }
        public static string ArtPath { get { return ""; } }

        public static string[] BanSpritePath { get { return PdrConfig.ToArray(""); } }
        public static string[] BanTexturePath { get { return PdrConfig.ToArray(""); } }
        #endregion

        #region GetAssets

        public static List<AssetObject> GetSourceSprites()
        {
            return GetSourceSprites(SourceAtlasPath, BanSpritePath);
        }

        public static List<AssetObject> GetAllPrefabs()
        {
            return GetPrefabs(AssetsRootPath);
        }

        public static List<AssetObject> GetUIPrefabs()
        {
            var l1 = GetPrefabs(UIPanelPath);
            var l2 = GetPrefabs(Config.UI_ASSETS_PATH + "/panel_lr");
            l1.AddRange(l2);
            return l1;
        }

        public static List<AssetObject> GetAllTextures()
        {
            return GetTextureImporters(AssetsRootPath, BanTexturePath);
        }

        public static List<AssetObject> GetArtTextures()
        {
            return GetTextureImporters(ArtPath);
        }

        public static List<AssetObject> GetFbxModels()
        {
            return GetFbxImporters(ArtPath);
        }

        public static List<string> GetUIPanelPaths()
        {
            List<string> list = new List<string>();
            FindDic(UIPanelPath, ".prefab", delegate (string path)
            {
                string name = Path.GetFileNameWithoutExtension(path);
                if (name != null && !name.EndsWith("Panel"))
                {
                    list.Add(path);
                }
            });
            return list;
        }
        #endregion

        #region Texture

        [Obsolete("该函数已丢失, 请使用GetAllTextures()", true)]
        public static List<string> GetAllTexturePath(string[] ignorePaths = null)
        {
            var guidArray = AssetDatabase.FindAssets("t:Texture");
            return guidArray.Select(AssetDatabase.GUIDToAssetPath).Where(path => ignorePaths == null || !CheckInIgnorPath(ignorePaths, path)).ToList();
        }

        [Obsolete]
        private static bool CheckInIgnorPath(string[] ignorePaths, string path)
        {
            return ignorePaths.Any(ignore => ignore == path);
        }
        #endregion

        #region Defend

        public static string[] GetDependPaths(Object go)
        {
            string sourcePath = AssetDatabase.GetAssetPath(go);
            return AssetDatabase.GetDependencies(sourcePath);
        }
        #endregion
    }
}
